<?php
class ModelConfigSubscribe extends Model {
	public function addSubscribe($data) {
		
		
		$abpicUrl = "";
		if ($data['pic_url'] && file_exists(DIR_IMAGE . $data['pic_url'])) {
			$abpicUrl ='image/'.$data['pic_url'];
		}
		
		$sql = "INSERT INTO `" . DB_PREFIX . "config_subscribe` SET ".
		"title = '" . $this->db->escape($data['title']) . "', ".
		"pic_url = '" . $this->db->escape($data['pic_url']) . "', ".
		"url = '" . $this->db->escape($data['url']) . "', ".
		"ab_pic_url = '" . $this->db->escape($abpicUrl) . "', ".
		"summary = '" . $this->db->escape($data['summary']) . "', ".
		"description = '" . $this->db->escape($data['description']) . "'";
		$this->db->query($sql);
	}
	
	public function editSubscribe($subscribe_id, $data) {
		$abpicUrl = "";
		if ($data['pic_url'] && file_exists(DIR_IMAGE . $data['pic_url'])) {
			$abpicUrl = 'image/'.$data['pic_url'];
		}
		$this->db->query("UPDATE `" . DB_PREFIX . "config_subscribe` SET ".
		"title = '" . $this->db->escape($data['title']) . "', ".
		"pic_url = '" . $this->db->escape($data['pic_url']) . "', ".
		"url = '" . $this->db->escape($data['url']) . "', ".
		"ab_pic_url = '" . $this->db->escape($abpicUrl) . "', ".
		"summary = '" . $this->db->escape($data['summary']) . "', ".
		"description = '" . $this->db->escape($data['description']) . "' WHERE subscribe_id = '" . (int)$subscribe_id . "'");
	}
	
	
	public function getTotalSubscribes() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_subscribe`");
		return $query->row['total'];
	}
	
	public function getSubscribe($subscribe_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "config_subscribe` WHERE subscribe_id = '" . (int)$subscribe_id . "'");
		return $query->row;
	}
	
	public function deleteSubscribe($subscribe_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "config_subscribe` WHERE subscribe_id = '" . (int)$subscribe_id . "'");
	}
	
	public function getSubscribes($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_subscribe`";
			
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getAllSubscribes($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_subscribe`";
			
		$sort_data = array(
			'date_added'
		);	
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
}
?>